package com.skywolf.chem.manager.controller;

import com.skywolf.chem.article.entity.Article;
import com.skywolf.chem.article.entity.ContestNotice;
import com.skywolf.chem.article.entity.LiveInfo;
import com.skywolf.chem.article.service.IArticleService;
import com.skywolf.chem.article.service.IContestNoticeService;
import com.skywolf.chem.article.service.ILiveInfoService;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.pageconfig.entity.LoopImg;
import com.skywolf.chem.pageconfig.service.ILoopImgService;
import com.skywolf.chem.utils.SysConfig;
import com.skywolf.chem.utils.file.UploadFile;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Created by pei_pei on 2020/6/29.
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/manager/file")
public class FileController {


    @Autowired
    IArticleService articleService;

    @Autowired
    ILiveInfoService liveInfoService;

    @Autowired
    IContestNoticeService noticeService;

    @Autowired
    ILoopImgService loopImgService;

    @ApiOperation(value = "上传封面图")
    @ResponseBody
    @RequestMapping(value = "/upload",method = {RequestMethod.POST,RequestMethod.GET}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private RespData<String> upload(@RequestParam MultipartFile file,
                                    @RequestParam(value = "articleId") Long articleId,
                                    HttpServletRequest request) {
        String url = "";
        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "img/"+UUID.randomUUID().toString()+suffix;
            new UploadFile().upload(file.getInputStream(),filePath);

            String host = SysConfig._this.getProperty("aliyunfile.urlhost");
            url = host+filePath;

            Article article = articleService.getById(articleId);
            String img = article.getCoverUrl();
            if(StringUtils.isNotBlank(img)){
                String[] list = img.split(",");
                if(list.length>3){
                    return RespData.failure(ResponseCodeEnum.IMG_ERROR);
                }
                img = img+","+url;
                article.setCoverUrl(img);
                article.setCreatedTime(null);
                article.setAddTime(null);
                articleService.updateById(article);
            }else{
                article.setCreatedTime(null);
                article.setAddTime(null);
                article.setCoverUrl(url);
                articleService.updateById(article);
            }
        } catch (Exception e) {
            log.error("FileController.upload error:{}", e);
            return RespData.failure(ResponseCodeEnum.FASTDFS_ERROR);
        }
        return RespData.success(url);
    }



    @ResponseBody
    @RequestMapping(value = "/uploadVideo",method = {RequestMethod.POST,RequestMethod.GET}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private RespData<String> uploadVideo(@RequestParam(value = "articleId") Long articleId,@RequestParam MultipartFile file, HttpServletRequest request) {
        String url = "";
        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "video/"+UUID.randomUUID().toString()+suffix;
            new UploadFile().upload(file.getInputStream(),filePath);
            String host = SysConfig._this.getProperty("aliyunfile.urlhost");
            url = host+filePath;
            Article article = new Article();
            article.setId(articleId);
            article.setContent(url);
            article.setCreatedTime(null);
            article.setAddTime(null);
            articleService.updateById(article);
        } catch (Exception e) {
            log.error("FileController.upload error:{}", e);
            return RespData.failure(ResponseCodeEnum.FASTDFS_ERROR);
        }
        return RespData.success(url);
    }


    @ApiOperation(value = "上传直播封面图")
    @ResponseBody
    @RequestMapping(value = "/uploadLiveImg",method = {RequestMethod.POST,RequestMethod.GET}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private RespData<String> uploadLiveImg(@RequestParam MultipartFile file,
                                    @RequestParam(value = "liveId") Long liveId,
                                    HttpServletRequest request) {
        String url = "";
        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "img/"+UUID.randomUUID().toString()+suffix;
            new UploadFile().upload(file.getInputStream(),filePath);

            String host = SysConfig._this.getProperty("aliyunfile.urlhost");
            url = host+filePath;
            LiveInfo live = liveInfoService.getById(liveId);
            live.setCoverUrl(url);
            live.setCreatedTime(null);
            liveInfoService.updateById(live);
        } catch (Exception e) {
            log.error("FileController.upload error:{}", e);
            return RespData.failure(ResponseCodeEnum.FASTDFS_ERROR);
        }
        return RespData.success(url);
    }

    @ApiOperation(value = "上传图片，返回图片url")
    @ResponseBody
    @RequestMapping(value = "/uploadImg",method = {RequestMethod.POST,RequestMethod.GET}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private RespData<String> uploadImg(@RequestParam(value = "file") MultipartFile file,
                                    HttpServletRequest request) {
        String url = "";
        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "img/"+UUID.randomUUID().toString()+suffix;
            new UploadFile().upload(file.getInputStream(),filePath);

            String host = SysConfig._this.getProperty("aliyunfile.urlhost");
            url = host+filePath;
            return RespData.success(url);
        } catch (Exception e) {
            log.error("FileController.upload error:{}", e);
        }
        return RespData.failure(ResponseCodeEnum.FASTDFS_ERROR);
    }


    @ApiOperation(value = "上传对标")
    @ResponseBody
    @RequestMapping(value = "/uploadTeamIcon",method = {RequestMethod.POST,RequestMethod.GET}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private RespData<String> uploadTeamIcon(@RequestParam MultipartFile file,
                                    @RequestParam(value = "noticeId") Long noticeId,
                                    @RequestParam(value = "type") Integer type,
                                    HttpServletRequest request) {
        String url = "";
        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "img/"+UUID.randomUUID().toString()+suffix;
            new UploadFile().upload(file.getInputStream(),filePath);
            String host = SysConfig._this.getProperty("aliyunfile.urlhost");
            url = host+filePath;

            ContestNotice notice = noticeService.getById(noticeId);
            if(type==1){
                notice.setTeamIcon1(url);
            }else{
                notice.setTeamIcon2(url);
            }
            notice.setCreatedTime(null);
            noticeService.updateById(notice);
        } catch (Exception e) {
            log.error("FileController.upload error:{}", e);
            return RespData.failure(ResponseCodeEnum.FASTDFS_ERROR);
        }
        return RespData.success(url);
    }


    @ApiOperation(value = "轮播图")
    @ResponseBody
    @RequestMapping(value = "/uploadLoopImg",method = {RequestMethod.POST,RequestMethod.GET}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private RespData<String> uploadLoopImg(@RequestParam MultipartFile file,
                                            @RequestParam(value = "id") Long id,
                                            HttpServletRequest request) {
        String url = "";
        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "img/"+UUID.randomUUID().toString()+suffix;
            new UploadFile().upload(file.getInputStream(),filePath);
            String host = SysConfig._this.getProperty("aliyunfile.urlhost");
            url = host+filePath;
            LoopImg loopImg = new LoopImg();
            loopImg.setId(id);
            loopImg.setImgUrl(url);
            loopImg.setCreatedTime(null);
            loopImgService.updateById(loopImg);
        } catch (Exception e) {
            log.error("FileController.upload error:{}", e);
            return RespData.failure(ResponseCodeEnum.FASTDFS_ERROR);
        }
        return RespData.success(url);
    }


}
